package njupt.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import njupt.dao.GoodsDAO;
import njupt.vo.Goods;

public class GoodsDAOimp implements GoodsDAO{
	private Connection conn=null;
	private PreparedStatement ps=null ;
	private ResultSet rs=null;
	 Goods goods=new Goods();
    public GoodsDAOimp(Connection conn){
		this.conn=conn;
		System.out.println("");
		}
	public Map selectgoods(int goodsid) throws Exception {
		Map goodsmsg = new HashMap();
		
    try{	
    	
		String sql="SELECT goodsname,goodstype,price,photo,chuxiao,photourl,goodsmount,goodsintroduction,goodsdate FROM goodsinfo WHERE goodsid=?";
		//String sql1="select * from users";
		this.ps=this.conn.prepareStatement(sql);
		if(goodsid==2){
			System.out.println("goodsid==2");
		}
		this.ps.setInt(1, goodsid);
		
		   rs=this.ps.executeQuery();
		 //System.out.println("到了此处123");
		  if(rs.next()){
			  
			System.out.println("GOODsDAOimp  rs里 完成");
			for(int i=1;i<9;i++){
				goodsmsg.put(i, rs.getString(i));
			}
			goodsmsg.put(9, rs.getDate(9));
			/*goods.setGoodsname(rs.getString(1));
			goods.setGoodstype(rs.getString(2));
			goods.setPrice(rs.getString(3));
			goods.setPhoto(rs.getString(4));
			goods.setChuxiao(rs.getInt(5));
			goods.setPhotourl(rs.getString(6));
			goods.setGoodsmount(rs.getInt(7));
			goods.setGoodsintroduction(rs.getString(8));
			goods.setGoodsdate(rs.getDate(9));*/
		//	System.out.println(user.getName());
			//goodsmsg.add(goods);
		     }
		  else{System.out.println("ERROR!"+rs.next());}
			  
		}catch(Exception e){
			throw e;
		}
		return goodsmsg;
	}
	public List<String[]> selectgoodsByType(String goodstype) throws Exception {
		List<String[]> goodsnum=new ArrayList<String[]>();   //goodsnum表示选择之后的产品种类数
		try{
		String sql="SELECT goodsid,goodsname,price,photo,chuxiao,photourl,goodsmount,goodsintroduction,goodsdate FROM goodsinfo WHERE goodstype=?";
		this.ps=this.conn.prepareStatement(sql);
		this.ps.setString(1, goodstype);
		this.rs=this.ps.executeQuery();
		
		ResultSetMetaData rsmd=rs.getMetaData();
		int columncount=rsmd.getColumnCount();
		System.out.println("columncount"+columncount);
		
		while(rs.next()){
			String[] goods=new String[columncount];
			for(int i=0;i<columncount;i++){
				goods[i]=rs.getString(i+1);
			}
			goodsnum.add(goods);
		}
		
		}catch(Exception e){
			throw e;
		}
		System.out.println("goodsnum"+goodsnum.size());
		return goodsnum;				  
	}

  
}
